//
//  String+Extension.swift
//  AQDeal
//
//  Created by zhangjikuan on 2020/5/28.
//  Copyright © 2020 hsgd. All rights reserved.
//

import Foundation
extension String {
    
    
    /**
     String 的 length
     
     - returns: Int
     */
    var length:Int {
        get{return (self as NSString).length}
    }
    
    /// 正则替换字符
    func replacing(pattern:String, template:String) ->String {
        
        if isEmpty {return self}
        
        do {
            let regularExpression = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
            
            return regularExpression.stringByReplacingMatches(in: self, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, length), withTemplate: template)
            
        } catch {return self}
    }
    
    /**
     截取字符串
     
     - returns: String
     */
    func substring(_ range:NSRange) ->String {
        
        return NSString(string: self).substring(with: range)
    }
    
}
